package org.utils.ui.xml;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

import org.apache.log4j.Logger;
import org.utils.ui.SwitchingChoicePopup;
import org.utils.ui.treetable.JTreeTable;
import org.utils.ui.treetable.TreeTableDataModel;
import org.utils.ui.treetable.TreeTableModel;
import org.utils.ui.treetable.TreeTableModelAdapter;

/**
 * @author Manuel EVENO
 */
public class XMLVisualizer extends JTreeTable implements ActionListener, MouseListener
{
	private static final long serialVersionUID = -3526035386444476305L;
	public static XMLVisualizer createXmlViewer(XMLDataTreeNode root)
	{
		String[] columnNames = { "Tag", "Value" };
		String[] getterMethodNames = { "toString", "getValue" };
		String[] setterMethodNames = new String[0];//{ "", "setValue" }; //
		Class[] cTypes = { TreeTableModel.class, String.class }; //EventNode.class
		TreeTableDataModel model = new TreeTableDataModel(
			root,
			columnNames,
			getterMethodNames,
			setterMethodNames,
			cTypes
		);
		int[] cellEditable = { 0 };
		model.setCellEditable( cellEditable );
		return new XMLVisualizer(model, root);
	}
	public static XMLVisualizer createXmlEditor(XMLDataTreeNode root)
	{
		TreeTableDataModel model = createDataModel(root);
		return new XMLVisualizer(model, root);
	}
	/**
	 * @param root
	 * @return
	 */
	private static TreeTableDataModel createDataModel(XMLDataTreeNode root)
	{
		String[] columnNames = { "Tag", "Value" };
		String[] getterMethodNames = { "toString", "getValue" };
		String[] setterMethodNames = { "setName", "setValue" }; //new String[0];//
		Class[] cTypes = { TreeTableModel.class, String.class }; //EventNode.class
		TreeTableDataModel model = new TreeTableDataModel(
			root,
			columnNames,
			getterMethodNames,
			setterMethodNames,
			cTypes
		);
		int[] cellEditable = { 0, 1 };
		model.setCellEditable( cellEditable );
		return model;
	}

	private SwitchingChoicePopup popup = null;
	/**
	 * Constructor XMLViewer.
	 * @param model
	 * @param root
	 */
	public XMLVisualizer(TreeTableDataModel pModel, XMLDataTreeNode pRoot)
	{
		super(pModel);
		getTree().addMouseListener(this);
		getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		popup = new SwitchingChoicePopup("Expand", "Collapse");
		popup.addActionListener(this);

		MouseListener[] listeners = getTree().getMouseListeners();
		for (int i = 0; i < listeners.length; i++)
		{
			Logger.getLogger(getClass()).debug(listeners[i].getClass());
		}
	}
	/**
	 * 
	 * @param pRoot
	 */	
	public void updateData(XMLDataTreeNode pRoot)
	{
		setModel(
			new TreeTableModelAdapter(createDataModel(pRoot), getTree())
		);
	}
	/**
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		if (event.getActionCommand().equals("Expand") )
		{
			getTree().expandPath( getTree().getSelectionPath() );
			return ;
		}
		if (event.getActionCommand().equals("Collapse") )
		{
			getTree().collapsePath( getTree().getSelectionPath() );
			return ;
		}
	}
	/**
	 * 
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent e)
	{
//		Logger.getLogger(getClass()).debug("Event received: "+e);
		Logger.getLogger(getClass()).debug("isRightMouseButton: "+SwingUtilities.isRightMouseButton(e));
		if (SwingUtilities.isRightMouseButton(e))
		{
			int row = getTree().getRowForLocation(e.getX(), e.getY());
			if (row >= 0)
			{
				getTree().setSelectionRow(row);
				popup.switchSelection(getTree().isExpanded(row));
				popup.show(this, e.getX(), e.getY());
			}
		}
	}

	public void mousePressed(MouseEvent e) { }
	public void mouseReleased(MouseEvent e) { }
	public void mouseEntered(MouseEvent e) { }
	public void mouseExited(MouseEvent e) { }
}
